import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

def read_flm_data(file_path):
    x_coords = []
    data = []

    with open(file_path, 'r') as file:
        # Process the first line (X values)
        x_coords = list(map(float, file.readline().strip().split()[1:]))

        # Process the data section
        for line in file:
            row_data = list(map(float, line.strip().split()[1:]))
            data.append(row_data)

    return x_coords, data


if __name__ == "__main__":
    file_path = "D:\workspace\pythonPro\\flmProcess\data\I_1.5_2.5_4_3_3_m_invertx.flm"
    x_coords, data = read_flm_data(file_path)

    # Convert data and x_coords to numpy arrays
    data_array = np.array(data)
    y_coords, x_coords = np.mgrid[-59:60, -39:40]

    # Define your custom value-to-color mapping here
    # In this example, we create a colormap with 5 colors (blue to red)
    colors = ['blue', 'cyan', 'green', 'yellow', 'red']
    cmap = ListedColormap(colors)

    # Create the image plot
    plt.imshow(data_array, cmap=cmap, extent=[x_coords.min(), x_coords.max(), y_coords.min(), y_coords.max()],
               aspect='auto', origin='lower')
    plt.colorbar()  # Add a colorbar to the plot
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.title('Array Visualization')

    # Create meshgrid of x_coords and y_coords with the same dimensions as data_array
    x_mesh, y_mesh = np.meshgrid(x_coords, y_coords)

    # Plot each point at its real coordinate
    plt.scatter(x_coords, y_coords, c=data_array, cmap=cmap, marker='o', edgecolor='black')

    # Define the coordinates you want to annotate
    x_points = [x_coords.min(), x_coords.max(), 0]
    y_points = [y_coords.min(), y_coords.max(), 0]
    annotations = ['Xmin', 'Xmax', 'ZERO POINT']

    for x, y, annotation in zip(x_points, y_points, annotations):
        plt.text(x, y, annotation, color='white', ha='center', va='center', fontsize=8)

    # Customize the ticks and labels
    x_ticks = np.linspace(x_coords.min(), x_coords.max(), 5)
    y_ticks = np.linspace(y_coords.min(), y_coords.max(), 5)
    plt.xticks(x_ticks)
    plt.yticks(y_ticks)

    plt.show()
